<?php

/**
 * wishs actions.
 *
 * @package    sfgift
 * @subpackage wishs
 * @author     Tomislav
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class wishsActions extends sfActions
{
    public function preExecute() {
        parent::preExecute();
        if(!$this->getUser()->isAuthenticated())
                $this->redirect($this->generateUrl('fb_connect'));
    }
  public function executeIndex(sfWebRequest $request)
  {
    $this->wishs = Doctrine_Core::getTable('Wish')
      ->createQuery('w')
//      ->addWhere('w.sf_guard_user_id = ?', $this->getUser()->getGuardUser()->getId())
      ->execute();
//    var_dump($this->getUser()->getMelody('facebook')->getMe());
    
  }
  public function executeMy_wishs(sfWebRequest $request)
  {
    $this->wishs = Doctrine_Core::getTable('Wish')
      ->createQuery('w')
      ->addWhere('w.sf_guard_user_id = ?', $this->getUser()->getGuardUser()->getId())
      ->execute();
    $this->setTemplate('index');
    
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->wish = Doctrine_Core::getTable('Wish')->findOneById(array($request->getParameter('id')));
    $this->forward404Unless($this->wish);
  }
  
  public function executeSubscribe(sfWebRequest $request)
  {
    $this->wish = Doctrine_Core::getTable('Wish')->findOneById(array($request->getParameter('id')));
    $this->forward404Unless($this->wish);
    
    // subscribe by creating a user-wish relation
    // if a relation already exist, do not create a new relation
    $relation = UserWishTable::getInstance()->createQuery('uw')
            ->where('Wish_id = ?', $this->wish->getId())
            ->andWhere('sf_guard_user_id = ?', $this->getUser()->getGuardUser()->getId())
            ->execute();
    if($relation->count() == 0)
    {
        $relation = new UserWish();
        $relation->setWishId($this->wish->getId());
        $relation->setSfGuardUserId($this->getUser()->getGuardUser()->getId());
        $relation->save();
    }

    $this->getUser()->setFlash('info', 'Uspješno ste se prijavili za želju: '.$this->wish->getName());
    $this->redirect($this->generateUrl('wishs'));    
  }
  
  public function executeUnsubscribe(sfWebRequest $request)
  {
    $this->wish = Doctrine_Core::getTable('Wish')->findOneById(array($request->getParameter('id')));
    $this->forward404Unless($this->wish);
    
    // unsubscribe by deleting a user-wish relation
    // if a relation doesn't exist, report an error
    $relation = UserWishTable::getInstance()->createQuery('uw')
            ->where('Wish_id = ?', $this->wish->getId())
            ->andWhere('sf_guard_user_id = ?', $this->getUser()->getGuardUser()->getId())
            ->execute();
    
    foreach($relation as $user)
    {
        $user->delete();
    }

    $this->getUser()->setFlash('info', 'Successfully unsubscribed for a wish');
    $this->redirect($this->generateUrl('wishs'));    
  }

  public function executeNew(sfWebRequest $request)
  {
    $options = array(
        'user_id' => $this->getUser()->getGuardUser()->getId()
    );
    $this->form = new WishForm(null, $options);

  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new WishForm();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($wish = Doctrine_Core::getTable('Wish')->find(array($request->getParameter('id'))), sprintf('Object wish does not exist (%s).', $request->getParameter('id')));
    $this->form = new WishForm($wish);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($wish = Doctrine_Core::getTable('Wish')->find(array($request->getParameter('id'))), sprintf('Object wish does not exist (%s).', $request->getParameter('id')));
    $this->form = new WishForm($wish);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($wish = Doctrine_Core::getTable('Wish')->find(array($request->getParameter('id'))), sprintf('Object wish does not exist (%s).', $request->getParameter('id')));
    $wish->delete();

    $this->redirect('wishs/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $wish = $form->save();

      $this->redirect('wishs/edit?id='.$wish->getId());
    }
  }
}
